#!/bin/bash
declare INSTALLVERSION
declare INSTALLFILE

isNeedInstall()
{
    local versioninfo=`java -version 2>&1`
    if [ $? -eq 0 ]
    then 
       local version=`java -version 2>&1 | awk '{if(NR==1){gsub(/"/,""); print $3}}'`
       if [[ $versioninfo == "openjdk"* || $versioninfo == *"IBM"* ]]
       then 
           echo "install openjdk or ibm jdk"
           return 1
        elif [[ "${INSTALLVERSION}" == "${version}" ]]
        then
           echo "The oracle jdk(${INSTALLVERSION}) has installed."
           return 0
        else
           echo "This machine installed oracle jdk, but the version is wrong."
           return 1
        fi
    else
        echo "This machine doesn't install oracle jdk."
        return 1
    fi
}

find_installfile()
{
   local filename=`ls |grep ^jdk.*\.rpm`
   if [[ -f ${filename} ]]
   then
       INSTALLFILE=${filename}
       return 0
   else
       echo "can't find jdk install file."
       exit 1
   fi   
}

create_javalink()
{
    local java_home=$1
    local links=(java javac javadoc javafxpackager javah javap javapackager java-rmi.cgi javaws)
    for linkname in ${links[@]}
    do
       if [[ -f "/usr/bin/${linkname}" ]]
       then
          rm -f /usr/bin/${linkname}
       fi
         ln -s ${java_home}/bin/java /usr/bin/${linkname}
    done
}

install()
{
    if [ ! -f ${INSTALLFILE} ]
    then
       echo "the file ${INSTALLFILE} doesn't exits. "
       return 1
    else
       rpm -ivh ${INSTALLFILE}
       if [[ ! $? -eq 0 ]]
       then
          echo "install ${INSTALLFILE} error."
          return 1
       else
           local version=`java -version 2>&1 | awk '{if(NR==1){gsub(/"/,""); print $3}}'`
           if [[ ! "${version}" == "${INSTALLVERSION}" ]]
           then
              echo "create sybomic link."
              if [ -d "/usr/java" ]
              then
                 local java_home=`ls /usr/java | grep ${INSTALLVERSION}`
                 if [[ "$java_home" == "" ]]
                 then
                     echo "can't find the java install directory."
                     return 1
                 else
                     create_javalink "/usr/java/${java_home}"
                     version=`java -version 2>&1 | awk '{if(NR==1){gsub(/"/,""); print $3}}'`
                     if [[ "${version}" == "${INSTALLVERSION}" ]]
                     then
                        echo "install jdk successfully."
                        return 0
                     else
                        echo "unkown error. please check in manul."
                        return 1
                     fi
                 fi
              else
                 echo "/usr/java doesn't exist."
                 return 1
              fi  
           else
               echo "install jdk successfully."
               return 0
           fi
        fi
    fi    
}

if [ $# -eq 1 ]
then
   INSTALLVERSION=$1
   find_installfile
   isNeedInstall
   if [ $? -eq 1 ]
   then
      install
      if [ $? -eq 0 ]
      then
         exit 0
      else
         exit 1
      fi
   else
      exit 0
   fi 
else
   echo "params error.Usage: sh jdk_instanller.sh 1.8.0_181"
   exit 1
fi